# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def getDecimalValue(self, head: Optional[ListNode]) -> int:
        ans = 0
        cur = head #始终保证head指向头节点是不丢失的
        while cur is not None:
            ans *= 2
            ans += cur.val
            cur = cur.next #向后遍历
        return ans
        